package com.jilefojing.controller;

import com.google.common.collect.Maps;
import com.jilefojing.base.BaseWeb;
import com.jilefojing.beans.data.response.base.ResponseData;
import com.jilefojing.beans.data.web.response.RecommendData;
import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.PlatformEnum;
import com.jilefojing.beans.pojos.enhanced.EnhancedEArticle;
import com.jilefojing.beans.pojos.enhanced.EnhancedRecommend;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.convent.CommonConvent;
import com.jilefojing.service.IEArticleService;
import com.jilefojing.service.IEAudioService;
import com.jilefojing.service.IRecommendService;
import com.jilefojing.util.Detect;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("recommend")
public class RecommendController extends BaseWeb {
    @Autowired
    private IRecommendService recommendService;
    @Autowired
    private IEArticleService eArticleService;
    @Autowired
    private IEAudioService eAudioService;

    @PostMapping("list")
    public ResponseData list() {
        Pagination<EnhancedRecommend> enhancedRecommendArticlePagination = new Pagination<EnhancedRecommend>(1, 6, false);
        enhancedRecommendArticlePagination = recommendService.getPaginationForActive(PlatformEnum.PC, ItemTypeEnumForSource.ARTICLE, enhancedRecommendArticlePagination, null);


        ResponseData responseData = new ResponseData();
        Map<String, Object> result = Maps.newHashMap();

        if (Detect.notEmpty(enhancedRecommendArticlePagination.getItems())) {
            List<BaseEnhanced> enhancedItems = new ArrayList<BaseEnhanced>();
            for (EnhancedRecommend _EnhancedRecommend : enhancedRecommendArticlePagination.getItems()) {
                enhancedItems.add(_EnhancedRecommend.getEnhancedItem());
            }

            enhancedItems = (List) eArticleService.appendEnhancedDCategories((List) enhancedItems, null);


            String firstArticleImg = null;
            String firstArticleContent = "";

            EnhancedEArticle firstEnhancedEArticle = (EnhancedEArticle) enhancedRecommendArticlePagination.getItems().get(0).getEnhancedItem();
            Document firstDoc = Jsoup.parse(firstEnhancedEArticle.getContent());
            Elements firstImgs = firstDoc.select("img[src]");
            if (firstImgs.size() > 0) {
                firstArticleImg = firstImgs.get(0).attr("src").trim();
                firstImgs.remove();
            }
            Elements pElements = firstDoc.select("p");
            for (int i = 0; i < pElements.size(); i++) {
                firstArticleContent += pElements.get(i).text().toString();
                if (firstArticleContent.length() > 499) {
                    break;
                }
            }
            firstArticleContent += "...";

            List<RecommendData> articleRecommends = CommonConvent.convent2RecommendDatas(enhancedRecommendArticlePagination.getItems());

            result.put("firstArticleImg", firstArticleImg);
            result.put("firstArticleContent", firstArticleContent);
            result.put("articleRecommends", articleRecommends);
        }


        Pagination<EnhancedRecommend> enhancedRecommendAudioPagination = new Pagination<EnhancedRecommend>(1, 8, false);
        enhancedRecommendAudioPagination = recommendService.getPaginationForActive(PlatformEnum.PC, ItemTypeEnumForSource.AUDIO, enhancedRecommendAudioPagination, null);
        if (Detect.notEmpty(enhancedRecommendAudioPagination.getItems())) {
            List<BaseEnhanced> enhancedItems = new ArrayList<BaseEnhanced>();
            for (EnhancedRecommend _EnhancedRecommend : enhancedRecommendAudioPagination.getItems()) {
                enhancedItems.add(_EnhancedRecommend.getEnhancedItem());
            }
            enhancedItems = (List) eAudioService.appendEnhancedChildren((List) enhancedItems, null);

            List<RecommendData> audioRecommends = CommonConvent.convent2RecommendDatas(enhancedRecommendAudioPagination.getItems());
            result.put("audioRecommends", audioRecommends);
        }

        responseData.setData(result);
        return responseData;
    }
}
